#!/bin/bash

echo "=========================================="
echo "  undo_log 表测试（SQL 成功执行后回滚）"
echo "=========================================="
echo ""

# 记录初始状态
echo "【1】记录初始状态"
echo "库存 product_id=1:"
docker exec springcloud-mysql mysql -uroot -proot123456 seata_storage \
  -e "SELECT product_id, used, residue FROM tb_storage WHERE product_id=1;" 2>/dev/null | tail -n +2

echo "账户 user_id=1:"
docker exec springcloud-mysql mysql -uroot -proot123456 seata_account \
  -e "SELECT user_id, used, residue FROM tb_account WHERE user_id=1;" 2>/dev/null | tail -n +2

echo "订单总数:"
BEFORE_COUNT=$(docker exec springcloud-mysql mysql -uroot -proot123456 seata_order \
  -e "SELECT COUNT(*) as count FROM tb_order;" 2>/dev/null | tail -n +2)
echo "$BEFORE_COUNT"

echo ""
echo "【2】清理之前的 undo_log（如果存在）"
docker exec springcloud-mysql mysql -uroot -proot123456 seata_order -e "DELETE FROM undo_log;" 2>/dev/null
docker exec springcloud-mysql mysql -uroot -proot123456 seata_storage -e "DELETE FROM undo_log;" 2>/dev/null
docker exec springcloud-mysql mysql -uroot -proot123456 seata_account -e "DELETE FROM undo_log;" 2>/dev/null

echo ""
echo "【3】发送回滚测试请求（SQL 成功执行后回滚）"
echo "请求参数: userId=1, productId=1, count=1, money=10.00"
echo ""

# 发送请求并捕获响应
RESULT=$(curl -s -X POST http://localhost:8001/order/create/rollback-test \
  -H "Content-Type: application/json" \
  -d '{"userId":1,"productId":1,"count":1,"money":10.00}')

echo "API 返回: $RESULT"
echo ""

# 立即检查 undo_log（在回滚完成前）
echo "【4】立即检查 undo_log（回滚前）"
echo "等待 1 秒后检查..."
sleep 1

echo ""
echo "seata_order 库 undo_log 数量:"
ORDER_COUNT=$(docker exec springcloud-mysql mysql -uroot -proot123456 seata_order \
  -e "SELECT COUNT(*) as count FROM undo_log;" 2>/dev/null | tail -n +2)
echo "$ORDER_COUNT"

echo ""
echo "seata_storage 库 undo_log 数量:"
STORAGE_COUNT=$(docker exec springcloud-mysql mysql -uroot -proot123456 seata_storage \
  -e "SELECT COUNT(*) as count FROM undo_log;" 2>/dev/null | tail -n +2)
echo "$STORAGE_COUNT"

echo ""
echo "seata_account 库 undo_log 数量:"
ACCOUNT_COUNT=$(docker exec springcloud-mysql mysql -uroot -proot123456 seata_account \
  -e "SELECT COUNT(*) as count FROM undo_log;" 2>/dev/null | tail -n +2)
echo "$ACCOUNT_COUNT"

echo ""
echo "【5】查看 undo_log 详细内容（如果存在）"
if [ "$ORDER_COUNT" -gt 0 ]; then
    echo "seata_order 库 undo_log 内容:"
    docker exec springcloud-mysql mysql -uroot -proot123456 seata_order \
      -e "SELECT branch_id, xid, log_status, log_created FROM undo_log LIMIT 1;" 2>/dev/null
fi

if [ "$STORAGE_COUNT" -gt 0 ]; then
    echo "seata_storage 库 undo_log 内容:"
    docker exec springcloud-mysql mysql -uroot -proot123456 seata_storage \
      -e "SELECT branch_id, xid, log_status, log_created FROM undo_log LIMIT 1;" 2>/dev/null
fi

if [ "$ACCOUNT_COUNT" -gt 0 ]; then
    echo "seata_account 库 undo_log 内容:"
    docker exec springcloud-mysql mysql -uroot -proot123456 seata_account \
      -e "SELECT branch_id, xid, log_status, log_created FROM undo_log LIMIT 1;" 2>/dev/null
fi

echo ""
echo "【6】验证数据是否回滚"
echo "库存 product_id=1（应该未变化）:"
docker exec springcloud-mysql mysql -uroot -proot123456 seata_storage \
  -e "SELECT product_id, used, residue FROM tb_storage WHERE product_id=1;" 2>/dev/null | tail -n +2

echo "账户 user_id=1（应该未变化）:"
docker exec springcloud-mysql mysql -uroot -proot123456 seata_account \
  -e "SELECT user_id, used, residue FROM tb_account WHERE user_id=1;" 2>/dev/null | tail -n +2

echo "订单总数（应该未增加）:"
AFTER_COUNT=$(docker exec springcloud-mysql mysql -uroot -proot123456 seata_order \
  -e "SELECT COUNT(*) as count FROM tb_order;" 2>/dev/null | tail -n +2)
echo "$AFTER_COUNT"

echo ""
echo "【结论】"
if echo "$RESULT" | grep -q '"code":500'; then
    echo "✅ API 正确返回失败"
    if [ "$BEFORE_COUNT" == "$AFTER_COUNT" ]; then
        echo "✅ 订单数量未增加"
        echo "✅ 数据回滚成功"
    else
        echo "❌ 订单数量发生变化，回滚失败！"
    fi

    echo ""
    echo "📝 undo_log 说明："
    if [ "$ORDER_COUNT" -eq 0 ] && [ "$STORAGE_COUNT" -eq 0 ] && [ "$ACCOUNT_COUNT" -eq 0 ]; then
        echo "• undo_log 为空是正常的"
        echo "• Seata 在回滚完成后会自动清理 undo_log"
        echo "• undo_log 只在回滚过程中短暂存在"
        echo "• 回滚完成后，Seata 会删除 undo_log 记录"
    else
        echo "• 发现 undo_log 记录"
        echo "• 这些记录会在回滚完成后被清理"
    fi
else
    echo "❌ API 没有返回失败，这是一个BUG！"
fi

echo ""
echo "=========================================="
echo "  ✅ undo_log 测试完成！"
echo "=========================================="
echo ""
echo "说明："
echo "  - SQL 成功执行后触发回滚"
echo "  - undo_log 记录 Before Image"
echo "  - 回滚完成后自动清理 undo_log"
echo "  - 这是 Seata AT 模式的正常行为"
echo ""
echo "下一步："
echo "  - 运行完整功能测试：./resources/scripts/04-Seata功能测试.sh"
echo "  - 查看 Seata 日志：docker logs -f springcloud-seata"

